package com.vmove.childtrain.fragment;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.vmove.childtrain.R;
import com.vmove.childtrain.domain.TeamData;
import com.vmove.childtrain.utils.TimeUtils;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;

import androidx.annotation.Nullable;
import androidx.core.math.MathUtils;
import androidx.fragment.app.BaseDialogFragment;
import androidx.fragment.app.DialogFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 多组得分显示框
 */
public class DegreeDialog_lesson2 extends BaseDialogFragment {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_win)
    TextView tvWin;
    @BindView(R.id.tv_timeCostRed)
    TextView tvTimeCostRed;
    @BindView(R.id.tv_timeCostYellow)
    TextView tvTimeCostYellow;
    @BindView(R.id.tv_timeCostBlue)
    TextView tvTimeCostBlue;
    @BindView(R.id.tv_timeCostGreen)
    TextView tvTimeCostGreen;
    @BindView(R.id.tv_successRed)
    TextView tvSuccessRed;
    @BindView(R.id.tv_successYellow)
    TextView tvSuccessYellow;
    @BindView(R.id.tv_successBlue)
    TextView tvSuccessBlue;
    @BindView(R.id.tv_successGreen)
    TextView tvSuccessGreen;
    @BindView(R.id.tv_failedRed)
    TextView tvFailedRed;
    @BindView(R.id.tv_failedYellow)
    TextView tvFailedYellow;
    @BindView(R.id.tv_failedBlue)
    TextView tvFailedBlue;
    @BindView(R.id.tv_failedGreen)
    TextView tvFailedGreen;
    private View rootView;

    Unbinder unbinder;
    private TeamData degreeRed;
    private TeamData degreeYellow;
    private TeamData degreeBlue;
    private TeamData degreeGreen;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.dialog_degree_lesson2, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        initView(rootView);
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Material_Light_Dialog_NoActionBar_MinWidth);
        setCancelable(false);
        Bundle args = getArguments();
        degreeRed = args.getParcelable("degreeRed");
        degreeYellow = args.getParcelable("degreeYellow");
        degreeBlue = args.getParcelable("degreeBlue");
        degreeGreen = args.getParcelable("degreeGreen");
    }

    /**
     * 定义控件
     *
     * @param rootView
     */
    private void initView(View rootView) {
        /*tvTimeCostRed.setText(TimeUtils.convertToDifftime(TimeUtils.FORMAT_DAY_CN_3, degreeRed.getTimeCost()));
        tvTimeCostYellow.setText(TimeUtils.convertToDifftime(TimeUtils.FORMAT_DAY_CN_3, degreeYellow.getTimeCost()));
        tvTimeCostBlue.setText(TimeUtils.convertToDifftime(TimeUtils.FORMAT_DAY_CN_3, degreeBlue.getTimeCost()));
        tvTimeCostGreen.setText(TimeUtils.convertToDifftime(TimeUtils.FORMAT_DAY_CN_3, degreeGreen.getTimeCost()));*/
        tvTimeCostRed.setText(formatTime(degreeRed.getTimeCost()));
        tvTimeCostYellow.setText(formatTime(degreeYellow.getTimeCost()));
        tvTimeCostBlue.setText(formatTime(degreeBlue.getTimeCost()));
        tvTimeCostGreen.setText(formatTime(degreeGreen.getTimeCost()));
        int redSuccess = degreeRed.getSuccess();
        int yellowSuccess = degreeYellow.getSuccess();
        int blueSuccess = degreeBlue.getSuccess();
        int greenSuccess = degreeGreen.getSuccess();
        tvSuccessRed.setText(redSuccess +"");
        tvSuccessYellow.setText(yellowSuccess+"");
        tvSuccessBlue.setText(blueSuccess+"");
        tvSuccessGreen.setText(greenSuccess+"");
        tvFailedRed.setText(degreeRed.getFailed()+"");
        tvFailedYellow.setText(degreeYellow.getFailed()+"");
        tvFailedBlue.setText(degreeBlue.getFailed()+"");
        tvFailedGreen.setText(degreeGreen.getFailed()+"");
        int max = getMax(redSuccess, yellowSuccess, blueSuccess, greenSuccess);
        if (redSuccess==max){
            tvWin.setText("红队获胜");
        }
        if (yellowSuccess==max){
            tvWin.setText("黄队获胜");
        }
        if (blueSuccess==max){
            tvWin.setText("蓝队获胜");
        }
        if (greenSuccess==max){
            tvWin.setText("绿队获胜");
        }
    }

    /**
     * 格式化 ms时间为 xx秒xx
     * @param miles
     * @return
     */
    private String formatTime(long miles){
        float time = miles/1000f;
        DecimalFormat df = new DecimalFormat("0.00");
        String format = df.format(time);
        String[] split = format.split("\\.");
        return split[0]+"秒"+split[1];
    }

    private int getMax(int redSuccess, int yellowSuccess, int blueSuccess, int greenSuccess) {
        int max=redSuccess;
        max = yellowSuccess>max?yellowSuccess:max;
        max = blueSuccess>max?blueSuccess:max;
        max = greenSuccess>max?greenSuccess:max;
        return max;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != unbinder) {
            unbinder.unbind();
        }
    }

    @OnClick(R.id.iv_close)
    public void onViewClicked() {
        this.dismiss();
    }
}
